package com.cjh.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cjh.common.R;
import com.cjh.dto.SetmealDto;
import com.cjh.entity.Category;
import com.cjh.entity.Dish;
import com.cjh.entity.Setmeal;
import com.cjh.entity.SetmealDish;
import com.cjh.service.CategoryService;
import com.cjh.service.SetmealDishService;
import com.cjh.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.websocket.server.PathParam;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Resource
    private SetmealService setmealService;
    @Resource
    private SetmealDishService setmealDishService;
    @Resource
    private CategoryService categoryService;

    //增
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        setmealService.saveSetmealWithDish(setmealDto);

        return R.success("数据保存成功");
    }

    //查找
    @GetMapping("page")
    public R<Page> page(Integer page, Integer pageSize, String name) {
        Page<Setmeal> setmealPage = new Page<>(page, pageSize);
        Page<SetmealDto> setmealDtoPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(name != null, Setmeal::getName, name);
        wrapper.orderByDesc(Setmeal::getUpdateTime);
        Page<Setmeal> pageInfo = setmealService.page(setmealPage, wrapper);
        BeanUtils.copyProperties(pageInfo, setmealDtoPage, "records");
        //处理page中的records集合将需要的数据加进去
        List<SetmealDto> records = pageInfo.getRecords().stream().map(item ->
                {     //创建DishDto类
                    SetmealDto setmealDto = new SetmealDto();
                    //将属性拷贝进dishDto中
                    BeanUtils.copyProperties(item, setmealDto);
                    //获取分类id
                    Long categoryId = item.getCategoryId();//分类id
                    //根据id查询分类对象
                    Category category = categoryService.getById(categoryId);
                    //如果查询到,给disDto设置值
                    if (category != null) {
                        String categoryName = category.getName();
                        setmealDto.setCategoryName(categoryName);
                    }
                    return setmealDto;
                }

        ).collect(Collectors.toList());

        setmealDtoPage.setRecords(records);


        return R.success(setmealDtoPage);
    }

    //修改时查询当前套餐信息
    @GetMapping("/{id}")
    public R<SetmealDto> get(@PathVariable Long id) {

        return R.success(setmealService.getSetmealWithDishById(id));

    }

    //改
    @PutMapping
    public R<String> update(@RequestBody SetmealDto dto) {
        setmealService.updateWithDish(dto);
        return R.success("修改成功");
    }

    //删
    @DeleteMapping
    @Transactional
    public R<String> delete(@RequestParam List<Long> ids) {
        setmealService.removeByIds(ids);
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, ids);
        setmealDishService.removeById(wrapper);
        return R.success("删除成功");
    }

    //状态修改
    @PostMapping("status/{status}")
    public R<String> status(@PathVariable Integer status, @RequestParam List<Long> ids) {
        List<Setmeal> setmeals = setmealService.listByIds(ids);
        setmeals = setmeals.stream().map(item -> {
                    item.setStatus(status);
                    return item;
                }
        ).collect(Collectors.toList());
        setmealService.updateBatchById(setmeals);
        return R.success("状态修改成功");
    }

    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);
    }
}